// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <nebula/types/meta/fixed_size_binary_type.h>

namespace nebula {

    /// \brief Base type class for (fixed-size) decimal data
    class TURBO_EXPORT DecimalType : public FixedSizeBinaryType {
    public:
        explicit DecimalType(Type::type type_id, int32_t byte_width, int32_t precision,
                             int32_t scale)
                : FixedSizeBinaryType(byte_width, type_id), precision_(precision), scale_(scale) {}

        /// Constructs concrete decimal types
        static turbo::Result<std::shared_ptr<DataType>> create(Type::type type_id, int32_t precision,
                                                               int32_t scale);

        int32_t precision() const { return precision_; }

        int32_t scale() const { return scale_; }

        /// \brief Returns the number of bytes needed for precision.
        ///
        /// precision must be >= 1
        static int32_t DecimalSize(int32_t precision);

    protected:
        std::string compute_fingerprint() const override;

        int32_t precision_;
        int32_t scale_;
    };


    /// \brief Concrete type class for 128-bit decimal data
    ///
    /// Nebula decimals are fixed-point decimal numbers encoded as a scaled
    /// integer.  The precision is the number of significant digits that the
    /// decimal type can represent; the scale is the number of digits after
    /// the decimal point (note the scale can be negative).
    ///
    /// As an example, `Decimal128Type(7, 3)` can exactly represent the numbers
    /// 1234.567 and -1234.567 (encoded internally as the 128-bit integers
    /// 1234567 and -1234567, respectively), but neither 12345.67 nor 123.4567.
    ///
    /// Decimal128Type has a maximum precision of 38 significant digits
    /// (also available as Decimal128Type::kMaxPrecision).
    /// If higher precision is needed, consider using Decimal256Type.
    class TURBO_EXPORT Decimal128Type : public DecimalType {
    public:
        static constexpr Type::type type_id = Type::DECIMAL128;

        static constexpr const char *type_name() { return "decimal128"; }

        /// Decimal128Type constructor that aborts on invalid input.
        explicit Decimal128Type(int32_t precision, int32_t scale);

        /// Decimal128Type constructor that returns an error on invalid input.
        static turbo::Result<std::shared_ptr<DataType>> create(int32_t precision, int32_t scale);

        std::string to_string(bool show_metadata = false) const override;

        std::string name() const override { return "decimal128"; }

        static constexpr int32_t kMinPrecision = 1;
        static constexpr int32_t kMaxPrecision = 38;
        static constexpr int32_t kByteWidth = 16;
    };

    /// \brief Concrete type class for 256-bit decimal data
    ///
    /// Nebula decimals are fixed-point decimal numbers encoded as a scaled
    /// integer.  The precision is the number of significant digits that the
    /// decimal type can represent; the scale is the number of digits after
    /// the decimal point (note the scale can be negative).
    ///
    /// Decimal256Type has a maximum precision of 76 significant digits.
    /// (also available as Decimal256Type::kMaxPrecision).
    ///
    /// For most use cases, the maximum precision offered by Decimal128Type
    /// is sufficient, and it will result in a more compact and more efficient
    /// encoding.
    class TURBO_EXPORT Decimal256Type : public DecimalType {
    public:
        static constexpr Type::type type_id = Type::DECIMAL256;

        static constexpr const char *type_name() { return "decimal256"; }

        /// Decimal256Type constructor that aborts on invalid input.
        explicit Decimal256Type(int32_t precision, int32_t scale);

        /// Decimal256Type constructor that returns an error on invalid input.
        static turbo::Result<std::shared_ptr<DataType>> create(int32_t precision, int32_t scale);

        std::string to_string(bool show_metadata = false) const override;

        std::string name() const override { return "decimal256"; }

        static constexpr int32_t kMinPrecision = 1;
        static constexpr int32_t kMaxPrecision = 76;
        static constexpr int32_t kByteWidth = 32;
    };

}  // namespace nebula
